﻿using Juli.Mes.Customers.Dtos;
using Juli.Mes.VisionData;
using Juli.Mes.VisionDemo.Dtos;
using Juli.Mes.VisionDemo.Helper;
using Lion.AbpPro.NotificationManagement.Notifications;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OfficeOpenXml;
using OfficeOpenXml.FormulaParsing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using MongoDB.Bson;
using Volo.Abp;
using Volo.Abp.Domain.Repositories;


namespace Juli.Mes.VisionDemo
{
    public class VisionDemoAppService : MesAppService, IVisionDemoAppService
    {
        private readonly NotificationManager notificationManager;
        private readonly NotificationAppService notificationAppService;
        private readonly IWebHostEnvironment _webHostEnv;
        private readonly IRepository<SaleRecord> saleRecordRepository;

        public VisionDemoAppService(NotificationManager notificationManager, 
            NotificationAppService notificationAppService,
            IWebHostEnvironment webHostEnv,
             IRepository<SaleRecord> saleRecordRepository)
        {
            this.notificationManager = notificationManager;
            this.notificationAppService = notificationAppService;
            _webHostEnv = webHostEnv;
            this.saleRecordRepository = saleRecordRepository;
        }

        /// <summary>
        /// 设备状态统计 demo
        /// </summary>
        /// <returns></returns>
        public TotalStateDto GetTotalState()
        {
            var returnValue = new TotalStateDto();

            returnValue.MachineStates.Add(new StateDto()
            {
                Id = 1,
                Name = "报警",
                Value = RandomValueHelper.Get_intRandom(5, 0)
            });

            returnValue.MachineStates.Add(new StateDto()
            {
                Id = 2,
                Name = "充电",
                Value = RandomValueHelper.Get_intRandom(10, 2)
            });

            returnValue.MachineStates.Add(new StateDto()
            {
                Id = 3,
                Name = "空闲",
                Value = RandomValueHelper.Get_intRandom(10, 5)
            });

            returnValue.MachineStates.Add(new StateDto()
            {
                Id = 4,
                Name = "运行",
                Value = RandomValueHelper.Get_intRandom(15, 10)
            });

            returnValue.MachineStates.Add(new StateDto()
            {
                Id = 5,
                Name = "维修",
                Value = RandomValueHelper.Get_intRandom(3, 1)
            });

            return returnValue;
        }

        /// <summary>
        /// 设备报警demo
        /// </summary>
        /// <returns></returns>
        public TotalAlarmDto GetTotalAlarm()
        {
            var returnValue = new TotalAlarmDto();

            List<string> customList = ["宝钢", "吉安钢铁", "包钢"];
            List<string> textList = ["低电量", "低温", "充电机通信"];
            List<string> statusList = ["预警", "警报"];
            List<string> codeList = ["X", "Y", "Z"];
            returnValue.Alarms.Add(new AlarmDto()
            {
                Id = 1,
                Custom = RandomValueHelper.Get_stringRandom(customList) + RandomValueHelper.Get_stringRandom(codeList) + RandomValueHelper.Get_intRandom(100, 0).ToString().PadLeft(3, '0'),
                AlarmText = RandomValueHelper.Get_stringRandom(textList),
                AlarmStatus = RandomValueHelper.Get_stringRandom(statusList),
                CreationTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
            }); ;

            returnValue.Alarms.Add(new AlarmDto()
            {
                Id = 2,
                Custom = RandomValueHelper.Get_stringRandom(customList) + RandomValueHelper.Get_stringRandom(codeList) + RandomValueHelper.Get_intRandom(100, 0).ToString().PadLeft(3, '0'),
                AlarmText = RandomValueHelper.Get_stringRandom(textList),
                AlarmStatus = RandomValueHelper.Get_stringRandom(statusList),
                CreationTime = DateTime.Now.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm:ss")
            });

            returnValue.Alarms.Add(new AlarmDto()
            {
                Id = 3,
                Custom = RandomValueHelper.Get_stringRandom(customList) + RandomValueHelper.Get_stringRandom(codeList) + RandomValueHelper.Get_intRandom(100, 0).ToString().PadLeft(3, '0'),
                AlarmText = RandomValueHelper.Get_stringRandom(textList),
                AlarmStatus = RandomValueHelper.Get_stringRandom(statusList),
                CreationTime = DateTime.Now.AddMinutes(-1).AddSeconds(-20).ToString("yyyy-MM-dd HH:mm:ss")
            });

            returnValue.Alarms.Add(new AlarmDto()
            {
                Id = 4,
                Custom = RandomValueHelper.Get_stringRandom(customList) + RandomValueHelper.Get_stringRandom(codeList) + RandomValueHelper.Get_intRandom(100, 0).ToString().PadLeft(3, '0'),
                AlarmText = RandomValueHelper.Get_stringRandom(textList),
                AlarmStatus = RandomValueHelper.Get_stringRandom(statusList),
                CreationTime = DateTime.Now.AddMinutes(-2).AddSeconds(-2).ToString("yyyy-MM-dd HH:mm:ss")
            });

            returnValue.Alarms.Add(new AlarmDto()
            {
                Id = 5,
                Custom = RandomValueHelper.Get_stringRandom(customList) + RandomValueHelper.Get_stringRandom(codeList) + RandomValueHelper.Get_intRandom(100, 0).ToString().PadLeft(3, '0'),
                AlarmText = RandomValueHelper.Get_stringRandom(textList),
                AlarmStatus = RandomValueHelper.Get_stringRandom(statusList),
                CreationTime = DateTime.Now.AddMinutes(-2).AddSeconds(-52).ToString("yyyy-MM-dd HH:mm:ss")
            });

            returnValue.Alarms.Add(new AlarmDto()
            {
                Id = 6,
                Custom = RandomValueHelper.Get_stringRandom(customList) + RandomValueHelper.Get_stringRandom(codeList) + RandomValueHelper.Get_intRandom(100, 0).ToString().PadLeft(3, '0'),
                AlarmText = RandomValueHelper.Get_stringRandom(textList),
                AlarmStatus = RandomValueHelper.Get_stringRandom(statusList),
                CreationTime = DateTime.Now.AddMinutes(-3).AddSeconds(-25).ToString("yyyy-MM-dd HH:mm:ss")
            });

            returnValue.Alarms.Add(new AlarmDto()
            {
                Id = 7,
                Custom = RandomValueHelper.Get_stringRandom(customList) + RandomValueHelper.Get_stringRandom(codeList) + RandomValueHelper.Get_intRandom(100, 0).ToString().PadLeft(3, '0'),
                AlarmText = RandomValueHelper.Get_stringRandom(textList),
                AlarmStatus = RandomValueHelper.Get_stringRandom(statusList),
                CreationTime = DateTime.Now.AddMinutes(-4).AddSeconds(-17).ToString("yyyy-MM-dd HH:mm:ss")
            });

            return returnValue;
        }

        /// <summary>
        /// 地图数据demo
        /// </summary>
        /// <returns></returns>
        public MapDataDto GetMapData()
        {
            var returnValue = new MapDataDto();

            returnValue.VoltageLevel = new List<string>() { "钢水罐", "铁水罐", "新能源车", "冶金车", "倾翻车" };
            returnValue.Colors = new List<string>() { "#1DE9B6", "#F46E36", "#04B9FF", "#5DBD32", "#FFC809", "#FB95D5", "#BDA29A", "#6E7074", "#546570", "#C4CCD3" };

            returnValue.CategoryData = new Dictionary<string, List<NameValue>>();
            returnValue.TopData = new Dictionary<string, List<NameValue<List<double>>>>();

            //钢水罐
            var jsonCategoryStr1 = @"[
                    {
                        ""name"": ""北京"",
                        ""value"": 304
                    },
                    {
                        ""name"": ""天津"",
                        ""value"": 280
                    },
                    {
                        ""name"": ""河北"",
                        ""value"": 1895
                    },
                    {
                        ""name"": ""山西"",
                        ""value"": 560
                    },
                    {
                        ""name"": ""内蒙古"",
                        ""value"": 120
                    },
                    {
                        ""name"": ""辽宁"",
                        ""value"": 130
                    },
                    {
                        ""name"": ""吉林"",
                        ""value"": 114
                    },
                    {
                        ""name"": ""黑龙江"",
                        ""value"": 15
                    },
                    {
                        ""name"": ""上海"",
                        ""value"": 2657
                    },
                    {
                        ""name"": ""江苏"",
                        ""value"": 1741
                    },
                    {
                        ""name"": ""安徽"",
                        ""value"": 464
                    },
                    {
                        ""name"": ""浙江"",
                        ""value"": 530
                    },
                    {
                        ""name"": ""福建"",
                        ""value"": 365
                    },
                    {
                        ""name"": ""江西"",
                        ""value"": 375
                    },
                    {
                        ""name"": ""山东"",
                        ""value"": 317
                    },
                    {
                        ""name"": ""河南"",
                        ""value"": 551
                    },
                    {
                        ""name"": ""湖北"",
                        ""value"": 975
                    },
                    {
                        ""name"": ""湖南"",
                        ""value"": 314
                    },
                    {
                        ""name"": ""广东"",
                        ""value"": 1462
                    },
                    {
                        ""name"": ""广西"",
                        ""value"": 320
                    },
                    {
                        ""name"": ""海南"",
                        ""value"": 15
                    },
                    {
                        ""name"": ""重庆"",
                        ""value"": 99
                    },
                    {
                        ""name"": ""四川"",
                        ""value"": 64
                    },
                    {
                        ""name"": ""贵州"",
                        ""value"": 4
                    },
                    {
                        ""name"": ""云南"",
                        ""value"": 75
                    },
                    {
                        ""name"": ""陕西"",
                        ""value"": 85
                    },
                    {
                        ""name"": ""甘肃"",
                        ""value"": 47
                    },
                    {
                        ""name"": ""青海"",
                        ""value"": 15
                    },
                    {
                        ""name"": ""宁夏"",
                        ""value"": 19
                    },
                    {
                        ""name"": ""新疆"",
                        ""value"": 14
                    },
                    {
                        ""name"": ""西藏"",
                        ""value"": 50
                    }
                ]";

            //铁水罐
            var jsonCategoryStr2 = @"[
                    {
                        ""name"": ""北京"",
                        ""value"": 404
                    },
                    {
                        ""name"": ""天津"",
                        ""value"": 314
                    },
                    {
                        ""name"": ""河北"",
                        ""value"": 2065
                    },
                    {
                        ""name"": ""山西"",
                        ""value"": 660
                    },
                    {
                        ""name"": ""内蒙古"",
                        ""value"": 220
                    },
                    {
                        ""name"": ""辽宁"",
                        ""value"": 230
                    },
                    {
                        ""name"": ""吉林"",
                        ""value"": 194
                    },
                    {
                        ""name"": ""黑龙江"",
                        ""value"": 151
                    },
                    {
                        ""name"": ""上海"",
                        ""value"": 2457
                    },
                    {
                        ""name"": ""江苏"",
                        ""value"": 1941
                    },
                    {
                        ""name"": ""安徽"",
                        ""value"": 578
                    },
                    {
                        ""name"": ""浙江"",
                        ""value"": 640
                    },
                    {
                        ""name"": ""福建"",
                        ""value"": 399
                    },
                    {
                        ""name"": ""江西"",
                        ""value"": 460
                    },
                    {
                        ""name"": ""山东"",
                        ""value"": 348
                    },
                    {
                        ""name"": ""河南"",
                        ""value"": 664
                    },
                    {
                        ""name"": ""湖北"",
                        ""value"": 875
                    },
                    {
                        ""name"": ""湖南"",
                        ""value"": 641
                    },
                    {
                        ""name"": ""广东"",
                        ""value"": 1962
                    },
                    {
                        ""name"": ""广西"",
                        ""value"": 250
                    },
                    {
                        ""name"": ""海南"",
                        ""value"": 151
                    },
                    {
                        ""name"": ""重庆"",
                        ""value"": 97
                    },
                    {
                        ""name"": ""四川"",
                        ""value"": 66
                    },
                    {
                        ""name"": ""贵州"",
                        ""value"": 41
                    },
                    {
                        ""name"": ""云南"",
                        ""value"": 7
                    },
                    {
                        ""name"": ""陕西"",
                        ""value"": 85
                    },
                    {
                        ""name"": ""甘肃"",
                        ""value"": 49
                    },
                    {
                        ""name"": ""青海"",
                        ""value"": 55
                    },
                    {
                        ""name"": ""宁夏"",
                        ""value"": 67
                    },
                    {
                        ""name"": ""新疆"",
                        ""value"": 61
                    },
                    {
                        ""name"": ""西藏"",
                        ""value"": 57
                    }
                ]";

            //新能源车
            var jsonCategoryStr3 = @"[
                    {
                        ""name"": ""北京"",
                        ""value"": 14
                    },
                    {
                        ""name"": ""天津"",
                        ""value"": 40
                    },
                    {
                        ""name"": ""河北"",
                        ""value"": 220
                    },
                    {
                        ""name"": ""山西"",
                        ""value"": 14
                    },
                    {
                        ""name"": ""内蒙古"",
                        ""value"": 14
                    },
                    {
                        ""name"": ""辽宁"",
                        ""value"": 9
                    },
                    {
                        ""name"": ""吉林"",
                        ""value"": 30
                    },
                    {
                        ""name"": ""黑龙江"",
                        ""value"": 27
                    },
                    {
                        ""name"": ""上海"",
                        ""value"": 247
                    },
                    {
                        ""name"": ""江苏"",
                        ""value"": 229
                    },
                    {
                        ""name"": ""安徽"",
                        ""value"": 107
                    },
                    {
                        ""name"": ""浙江"",
                        ""value"": 50
                    },
                    {
                        ""name"": ""福建"",
                        ""value"": 30
                    },
                    {
                        ""name"": ""江西"",
                        ""value"": 147
                    },
                    {
                        ""name"": ""山东"",
                        ""value"": 60
                    },
                    {
                        ""name"": ""河南"",
                        ""value"": 45
                    },
                    {
                        ""name"": ""湖北"",
                        ""value"": 128
                    },
                    {
                        ""name"": ""湖南"",
                        ""value"": 96
                    },
                    {
                        ""name"": ""广东"",
                        ""value"": 147
                    },
                    {
                        ""name"": ""广西"",
                        ""value"": 2
                    },
                    {
                        ""name"": ""海南"",
                        ""value"": 0
                    },
                    {
                        ""name"": ""重庆"",
                        ""value"": 9
                    },
                    {
                        ""name"": ""四川"",
                        ""value"": 7
                    },
                    {
                        ""name"": ""贵州"",
                        ""value"": 2
                    },
                    {
                        ""name"": ""云南"",
                        ""value"": 0
                    },
                    {
                        ""name"": ""陕西"",
                        ""value"": 14
                    },
                    {
                        ""name"": ""甘肃"",
                        ""value"": 0
                    },
                    {
                        ""name"": ""青海"",
                        ""value"": 0
                    },
                    {
                        ""name"": ""宁夏"",
                        ""value"": 0
                    },
                    {
                        ""name"": ""新疆"",
                        ""value"": 0
                    },
                    {
                        ""name"": ""西藏"",
                        ""value"": 0
                    }
                ]";

            //冶金车
            var jsonCategoryStr4 = @"[
                    {
                        ""name"": ""北京"",
                        ""value"": 42
                    },
                    {
                        ""name"": ""天津"",
                        ""value"": 62
                    },
                    {
                        ""name"": ""河北"",
                        ""value"": 261
                    },
                    {
                        ""name"": ""山西"",
                        ""value"": 50
                    },
                    {
                        ""name"": ""内蒙古"",
                        ""value"": 15
                    },
                    {
                        ""name"": ""辽宁"",
                        ""value"": 12
                    },
                    {
                        ""name"": ""吉林"",
                        ""value"": 60
                    },
                    {
                        ""name"": ""黑龙江"",
                        ""value"": 47
                    },
                    {
                        ""name"": ""上海"",
                        ""value"": 316
                    },
                    {
                        ""name"": ""江苏"",
                        ""value"": 209
                    },
                    {
                        ""name"": ""安徽"",
                        ""value"": 117
                    },
                    {
                        ""name"": ""浙江"",
                        ""value"": 89
                    },
                    {
                        ""name"": ""福建"",
                        ""value"": 65
                    },
                    {
                        ""name"": ""江西"",
                        ""value"": 158
                    },
                    {
                        ""name"": ""山东"",
                        ""value"": 88
                    },
                    {
                        ""name"": ""河南"",
                        ""value"": 75
                    },
                    {
                        ""name"": ""湖北"",
                        ""value"": 163
                    },
                    {
                        ""name"": ""湖南"",
                        ""value"": 95
                    },
                    {
                        ""name"": ""广东"",
                        ""value"": 163
                    },
                    {
                        ""name"": ""广西"",
                        ""value"": 13
                    },
                    {
                        ""name"": ""海南"",
                        ""value"": 0
                    },
                    {
                        ""name"": ""重庆"",
                        ""value"": 14
                    },
                    {
                        ""name"": ""四川"",
                        ""value"": 24
                    },
                    {
                        ""name"": ""贵州"",
                        ""value"": 4
                    },
                    {
                        ""name"": ""云南"",
                        ""value"": 0
                    },
                    {
                        ""name"": ""陕西"",
                        ""value"": 18
                    },
                    {
                        ""name"": ""甘肃"",
                        ""value"": 1
                    },
                    {
                        ""name"": ""青海"",
                        ""value"": 1
                    },
                    {
                        ""name"": ""宁夏"",
                        ""value"": 0
                    },
                    {
                        ""name"": ""新疆"",
                        ""value"": 0
                    },
                    {
                        ""name"": ""西藏"",
                        ""value"": 0
                    }
                ]";

            //倾翻车
            var jsonCategoryStr5 = @"[
                    {
                        ""name"": ""北京"",
                        ""value"": 62
                    },
                    {
                        ""name"": ""天津"",
                        ""value"": 77
                    },
                    {
                        ""name"": ""河北"",
                        ""value"": 365
                    },
                    {
                        ""name"": ""山西"",
                        ""value"": 71
                    },
                    {
                        ""name"": ""内蒙古"",
                        ""value"": 11
                    },
                    {
                        ""name"": ""辽宁"",
                        ""value"": 18
                    },
                    {
                        ""name"": ""吉林"",
                        ""value"": 92
                    },
                    {
                        ""name"": ""黑龙江"",
                        ""value"": 50
                    },
                    {
                        ""name"": ""上海"",
                        ""value"": 398
                    },
                    {
                        ""name"": ""江苏"",
                        ""value"": 248
                    },
                    {
                        ""name"": ""安徽"",
                        ""value"": 156
                    },
                    {
                        ""name"": ""浙江"",
                        ""value"": 140
                    },
                    {
                        ""name"": ""福建"",
                        ""value"": 80
                    },
                    {
                        ""name"": ""江西"",
                        ""value"": 177
                    },
                    {
                        ""name"": ""山东"",
                        ""value"": 14
                    },
                    {
                        ""name"": ""河南"",
                        ""value"": 99
                    },
                    {
                        ""name"": ""湖北"",
                        ""value"": 155
                    },
                    {
                        ""name"": ""湖南"",
                        ""value"": 123
                    },
                    {
                        ""name"": ""广东"",
                        ""value"": 189
                    },
                    {
                        ""name"": ""广西"",
                        ""value"": 15
                    },
                    {
                        ""name"": ""海南"",
                        ""value"": 0
                    },
                    {
                        ""name"": ""重庆"",
                        ""value"": 15
                    },
                    {
                        ""name"": ""四川"",
                        ""value"": 34
                    },
                    {
                        ""name"": ""贵州"",
                        ""value"": 6
                    },
                    {
                        ""name"": ""云南"",
                        ""value"": 0
                    },
                    {
                        ""name"": ""陕西"",
                        ""value"": 14
                    },
                    {
                        ""name"": ""甘肃"",
                        ""value"": 0
                    },
                    {
                        ""name"": ""青海"",
                        ""value"": 0
                    },
                    {
                        ""name"": ""宁夏"",
                        ""value"": 0
                    },
                    {
                        ""name"": ""新疆"",
                        ""value"": 0
                    },
                    {
                        ""name"": ""西藏"",
                        ""value"": 0
                    }
                ]";

            var itemCategory1 = JsonConvert.DeserializeObject<List<NameValue>>(jsonCategoryStr1);
            var itemCategory2 = JsonConvert.DeserializeObject<List<NameValue>>(jsonCategoryStr2);
            var itemCategory3 = JsonConvert.DeserializeObject<List<NameValue>>(jsonCategoryStr3);
            var itemCategory4 = JsonConvert.DeserializeObject<List<NameValue>>(jsonCategoryStr4);
            var itemCategory5 = JsonConvert.DeserializeObject<List<NameValue>>(jsonCategoryStr5);

            returnValue.CategoryData.Add("钢水罐", itemCategory1);
            returnValue.CategoryData.Add("铁水罐", itemCategory2);
            returnValue.CategoryData.Add("新能源车", itemCategory3);
            returnValue.CategoryData.Add("冶金车", itemCategory4);
            returnValue.CategoryData.Add("倾翻车", itemCategory5);

            var jsonTopStr1 = @"  [
                    //{
                    //    ""name"": ""天津"",
                    //    ""value"": [
                    //        117.4219,
                    //        39.4189,
                    //        84.1
                    //    ]
                    //},
                    {
                        ""name"": ""上海"",
                        ""value"": [
                            121.4648,
                            31.2891,
                            51.9
                        ]
                    },
                    {
                        ""name"": ""山西"",
                        ""value"": [
                            112.3352,
                            37.9413,
                            50.7
                        ]
                    },
                    {
                        ""name"": ""河北"",
                        ""value"": [
                            114.4995,
                            38.1006,
                            82.4
                        ]
                    },
                    {
                        ""name"": ""北京"",
                        ""value"": [
                            116.4551,
                            40.2539,
                            79.5
                        ]
                    },
                    {
                        ""name"": ""河南"",
                        ""value"": [
                            113.4668,
                            34.6234,
                            77.2
                        ]
                    },
                    {
                        ""name"": ""山东"",
                        ""value"": [
                            117.1582,
                            36.8701,
                            71.2
                        ]
                    },
                    //{
                    //    ""name"": ""安徽"",
                    //    ""value"": [
                    //        117.29,
                    //        32.0581,
                    //        68
                    //    ]
                    //},
                    {
                        ""name"": ""湖北"",
                        ""value"": [
                            114.3896,
                            30.6628,
                            64.5
                        ]
                    },
                    {
                        ""name"": ""江苏"",
                        ""value"": [
                            118.8062,
                            31.9208,
                            62.5
                        ]
                    },
                    //{
                    //    ""name"": ""新疆"",
                    //    ""value"": [
                    //        87.9236,
                    //        43.5883,
                    //        59.1
                    //    ]
                    //},
                    {
                        ""name"": ""湖南"",
                        ""value"": [
                            113.0823,
                            28.2568,
                            57.2
                        ]
                    }
                ] ";

            var jsonTopStr2 = @"[
                    //{
                    //    ""name"": ""北京"",
                    //    ""value"": [
                    //        116.4551,
                    //        40.2539,
                    //        78.5
                    //    ]
                    //},
                    {
                        ""name"": ""河北"",
                        ""value"": [
                            114.4995,
                            38.1006,
                            74.8
                        ]
                    },
                    {
                        ""name"": ""河南"",
                        ""value"": [
                            113.4668,
                            34.6234,
                            73.7
                        ]
                    },
                    {
                        ""name"": ""天津"",
                        ""value"": [
                            117.4219,
                            39.4189,
                            70.6
                        ]
                    },
                    {
                        ""name"": ""山东"",
                        ""value"": [
                            117.1582,
                            36.8701,
                            69.7
                        ]
                    },
                    //{
                    //    ""name"": ""安徽"",
                    //    ""value"": [
                    //        117.29,
                    //        32.0581,
                    //        58.9
                    //    ]
                    //},
                    {
                        ""name"": ""江苏"",
                        ""value"": [
                            118.8062,
                            31.9208,
                            58.6
                        ]
                    },
                    {
                        ""name"": ""湖北"",
                        ""value"": [
                            114.3896,
                            30.6628,
                            57.8
                        ]
                    },
                    {
                        ""name"": ""上海"",
                        ""value"": [
                            121.4648,
                            31.2891,
                            51.9
                        ]
                    },
                    {
                        ""name"": ""山西"",
                        ""value"": [
                            112.3352,
                            37.9413,
                            50.7
                        ]
                    }
                ]";

            var jsonTopStr3 = @"[
                    //{
                    //    ""name"": ""北京"",
                    //    ""value"": [
                    //        116.4551,
                    //        40.2539,
                    //        69.5
                    //    ]
                    //},
                    //{
                    //    ""name"": ""天津"",
                    //    ""value"": [
                    //        117.4219,
                    //        39.4189,
                    //        68.4
                    //    ]
                    //},
                     {
                        ""name"": ""上海"",
                        ""value"": [
                            121.4648,
                            31.2891,
                            51.9
                        ]
                    },
                    {
                        ""name"": ""山西"",
                        ""value"": [
                            112.3352,
                            37.9413,
                            50.7
                        ]
                    },
                    {
                        ""name"": ""河北"",
                        ""value"": [
                            114.4995,
                            38.1006,
                            67.3
                        ]
                    },
                    //{
                    //    ""name"": ""河南"",
                    //    ""value"": [
                    //        113.4668,
                    //        34.6234,
                    //        65.7
                    //    ]
                    //},
                    {
                        ""name"": ""山东"",
                        ""value"": [
                            117.1582,
                            36.8701,
                            61.3
                        ]
                    },
                    //{
                    //    ""name"": ""新疆"",
                    //    ""value"": [
                    //        87.9236,
                    //        43.5883,
                    //        53.8
                    //    ]
                    //},
                    //{
                    //    ""name"": ""安徽"",
                    //    ""value"": [
                    //        117.29,
                    //        32.0581,
                    //        52.9
                    //    ]
                    //},
                    //{
                    //    ""name"": ""陕西"",
                    //    ""value"": [
                    //        109.1162,
                    //        34.2004,
                    //        51.1
                    //    ]
                    //},
                    {
                        ""name"": ""湖北"",
                        ""value"": [
                            114.3896,
                            30.6628,
                            50.5
                        ]
                    },
                    {
                        ""name"": ""山西"",
                        ""value"": [
                            112.3352,
                            37.9413,
                            50.4
                        ]
                    }
                ]";

            var jsonTopStr4 = @"[
                    {
                        ""name"": ""河南"",
                        ""value"": [
                            113.4668,
                            34.6234,
                            61
                        ]
                    },
                    {
                        ""name"": ""天津"",
                        ""value"": [
                            117.4219,
                            39.4189,
                            60.1
                        ]
                    },
                   {
                        ""name"": ""上海"",
                        ""value"": [
                            121.4648,
                            31.2891,
                            51.9
                        ]
                    },
                    {
                        ""name"": ""山西"",
                        ""value"": [
                            112.3352,
                            37.9413,
                            50.7
                        ]
                    },
                    {
                        ""name"": ""北京"",
                        ""value"": [
                            116.4551,
                            40.2539,
                            55.7
                        ]
                    },
                    {
                        ""name"": ""山东"",
                        ""value"": [
                            117.1582,
                            36.8701,
                            54.3
                        ]
                    },
                    {
                        ""name"": ""陕西"",
                        ""value"": [
                            109.1162,
                            34.2004,
                            50.2
                        ]
                    },
                    //{
                    //    ""name"": ""新疆"",
                    //    ""value"": [
                    //        87.9236,
                    //        43.5883,
                    //        49.7
                    //    ]
                    //},
                    {
                        ""name"": ""山西"",
                        ""value"": [
                            112.3352,
                            37.9413,
                            49.4
                        ]
                    },
                    {
                        ""name"": ""湖北"",
                        ""value"": [
                            114.3896,
                            30.6628,
                            48.5
                        ]
                    }
                ]";

            var jsonTopStr5 = @"[
                    {
                        ""name"": ""河南"",
                        ""value"": [
                            113.4668,
                            34.6234,
                            57.3
                        ]
                    },
                    {
                        ""name"": ""新疆"",
                        ""value"": [
                            87.9236,
                            43.5883,
                            53.5
                        ]
                    },
                    {
                        ""name"": ""河北"",
                        ""value"": [
                            114.4995,
                            38.1006,
                            53.1
                        ]
                    },
                    {
                        ""name"": ""天津"",
                        ""value"": [
                            117.4219,
                            39.4189,
                            51.6
                        ]
                    },
                    {
                        ""name"": ""北京"",
                        ""value"": [
                            116.4551,
                            40.2539,
                            50
                        ]
                    },
                    {
                        ""name"": ""安徽"",
                        ""value"": [
                            117.29,
                            32.0581,
                            49.9
                        ]
                    },
                    {
                        ""name"": ""山东"",
                        ""value"": [
                            117.1582,
                            36.8701,
                            47.7
                        ]
                    },
                    {
                        ""name"": ""江苏"",
                        ""value"": [
                            118.8062,
                            31.9208,
                            46.2
                        ]
                    },
                    {
                        ""name"": ""陕西"",
                        ""value"": [
                            109.1162,
                            34.2004,
                            44.2
                        ]
                    },
                    {
                        ""name"": ""山西"",
                        ""value"": [
                            112.3352,
                            37.9413,
                            43.9
                        ]
                    }
                ]";

            var jsonTop1 = JsonConvert.DeserializeObject<List<NameValue<List<double>>>>(jsonTopStr1);
            var jsonTop2 = JsonConvert.DeserializeObject<List<NameValue<List<double>>>>(jsonTopStr2);
            var jsonTop3 = JsonConvert.DeserializeObject<List<NameValue<List<double>>>>(jsonTopStr3);
            var jsonTop4 = JsonConvert.DeserializeObject<List<NameValue<List<double>>>>(jsonTopStr4);
            var jsonTop5 = JsonConvert.DeserializeObject<List<NameValue<List<double>>>>(jsonTopStr5);

            //returnValue.TopData.Add(jsonTop1);
            //returnValue.TopData.Add(jsonTop2);
            //returnValue.TopData.Add(jsonTop3);
            //returnValue.TopData.Add(jsonTop4);
            //returnValue.TopData.Add(jsonTop5);

            returnValue.TopData.Add("钢水罐", jsonTop1);
            returnValue.TopData.Add("铁水罐", jsonTop2);
            returnValue.TopData.Add("新能源车", jsonTop3);
            returnValue.TopData.Add("冶金车", jsonTop4);
            returnValue.TopData.Add("倾翻车", jsonTop5);

            return returnValue;
        }

        /// <summary>
        /// 根据客户返回设备状态汇总
        /// </summary>
        /// <param name="customName"></param>
        /// <returns></returns>
        [HttpGet]
        public List<NameValue<int>> GetCustomState(string customName)
        {
            //运行 46B7FD
            //充电 4EE7B6
            //停机 F9CA2A  
            //报警 EB733E
            //待机 69C33B
            var returnValue = new List<NameValue<int>>();
            if (customName == "宝钢")
            {
                returnValue.Add(new NameValue<int>("运行", 6));//SH-Y-001 ,SH-Y-002,SH-Y-003,SH-Y-004,SH-Y-005,SH-Y-006 ,
                returnValue.Add(new NameValue<int>("充电", 1));//SH-C-007
                returnValue.Add(new NameValue<int>("报警", 1));//SH-B-008
                returnValue.Add(new NameValue<int>("待机", 3));//SH-D-009,SH-D-010,SH-D-011
            }
            else if (customName == "吉安钢铁")
            {
                returnValue.Add(new NameValue<int>("运行", 2));//JA-Y-001 ,BG-Y-002
            }
            else if (customName == "包钢")
            {
                returnValue.Add(new NameValue<int>("运行", 4));//BT-Y-001 ,BT-Y-002,BT-Y-003,BT-Y-004
                returnValue.Add(new NameValue<int>("充电", 1));//BT-C-005
                returnValue.Add(new NameValue<int>("停机", 1));//BT-T-006
                returnValue.Add(new NameValue<int>("报警", 1));//BT-B-009
                returnValue.Add(new NameValue<int>("待机", 2));//BT-D-007,JA-D-008
            }
            else
            {
                returnValue.Add(new NameValue<int>("运行", 1));
                returnValue.Add(new NameValue<int>("充电", 1));
                returnValue.Add(new NameValue<int>("停机", 1));
                returnValue.Add(new NameValue<int>("报警", 1));
                returnValue.Add(new NameValue<int>("待机", 1));
            }

            return returnValue;

        }

        /// <summary>
        /// 新能源电池组运行记录
        /// </summary>
        /// <returns></returns>
        public TotalBatteryInfoDto GetBatterySummary()
        {
            var returnValue = new TotalBatteryInfoDto();

            returnValue.BatteryInfos.Add(new BatteryInfoDto()
            {
                Id = 1,
                Name = "产品总数",
                Value = 27,
                Unit = "台"
            });

            returnValue.BatteryInfos.Add(new BatteryInfoDto()
            {
                Id = 2,
                Name = "单台最长使用",
                Value = 20,
                Unit = "分"
            });

            returnValue.BatteryInfos.Add(new BatteryInfoDto()
            {
                Id = 3,
                Name = "累计充放电次数",
                Value = 221,
                Unit = "次"
            });

            returnValue.BatteryInfos.Add(new BatteryInfoDto()
            {
                Id = 4,
                Name = "单日最高充放电次数",
                Value = 2,
                Unit = "次"
            });

            returnValue.BatteryInfos.Add(new BatteryInfoDto()
            {
                Id = 5,
                Name = "使用最高温度记录",
                Value = 45,
                Unit = "℃"
            });

            return returnValue;
        }

        /// <summary>
        /// 获取一笔报警
        /// </summary>
        /// <returns></returns>
        public AlarmDto GetOneAlarm()
        {
            List<string> customList = ["包钢BT-B-009", "宝钢SH-B-010"];
            List<string> textList = ["低电量"];
            List<string> statusList = ["预警"];
            List<string> codeList = ["X", "Y", "Z"];

            var item = new AlarmDto()
            {
                Id = 1,
                Custom = RandomValueHelper.Get_stringRandom(customList),
                AlarmText = RandomValueHelper.Get_stringRandom(textList),
                AlarmStatus = RandomValueHelper.Get_stringRandom(statusList),
                CreationTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
            };

            return item;
        }

        /// <summary>
        /// 产品市场占比 demo
        /// </summary>
        /// <returns></returns>
        public TotalStateDto GetMarketShare()
        {
            var returnValue = new TotalStateDto();

            returnValue.MachineStates.Add(new StateDto()
            {
                Id = 1,
                Name = "钢水罐",
                Value = 88
            });

            returnValue.MachineStates.Add(new StateDto()
            {
                Id = 2,
                Name = "铁水罐",
                Value = 91
            });

            returnValue.MachineStates.Add(new StateDto()
            {
                Id = 3,
                Name = "新能源车",
                Value = 50
            });

            returnValue.MachineStates.Add(new StateDto()
            {
                Id = 4,
                Name = "冶金车",
                Value = 78
            });

            returnValue.MachineStates.Add(new StateDto()
            {
                Id = 5,
                Name = "倾翻车",
                Value = 67
            });

            return returnValue;
        }

        public async Task<bool> SendAlarmMessage()
        {
            var alarm = this.GetOneAlarm();

            try
            {
                await notificationManager.SendBroadCastErrorMessageAsync("设备报警", @$"{alarm.Custom}:{alarm.AlarmText}");
                return true;
            }
            catch { return false; }

        }

        /// <summary>
        /// 电压折线图
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<TotalVoltageDto> GetVoltageData()
        {
            var returnValue = new List<TotalVoltageDto>();

            List<string> nameList = ["吉安钢铁-JA-C-006", "包钢-BT-C-006", "宝钢-SH-C-006"];

            foreach (var name in nameList)
            {
                var dto = new TotalVoltageDto()
                {
                    Name = name
                };
                var itemList = new List<VoltageDto>();

                for (int i = 1; i <= 96; i++)
                {
                    var item = new VoltageDto()
                    {
                        Location = i.ToString() + "#",
                        Voltage = RandomValueHelper.Get_doubleRandom(32600, 32540)
                    };
                    itemList.Add(item);
                }

                //算个总数
                dto.TotalVoltage = itemList.Sum(x => x.Voltage);
                dto.TotalAmmeter = RandomValueHelper.Get_doubleRandom(80000, -70000);
                dto.Max = itemList.OrderByDescending(i => i.Voltage).FirstOrDefault();
                dto.Min = itemList.OrderBy(i => i.Voltage).FirstOrDefault();
                dto.VoltageList = itemList;

                returnValue.Add(dto);
            }

            return returnValue;
        }

        [HttpGet]
        public string LocalizableStringTest()
        {
            return L["LongWelcomeMessage"];
        }

        public MonthTimeDto GetMonthTime(string customName)
        {
            var returnValue = new MonthTimeDto();
            if (customName == "宝钢")
            {
                returnValue.MachineNameList.Add("SH-Y-001"); 
                returnValue.RunTimeList.Add(69);
                returnValue.PowerTimeList.Add(0.5);

                returnValue.MachineNameList.Add("SH-Y-002");
                returnValue.RunTimeList.Add(66);
                returnValue.PowerTimeList.Add(0.45);

                returnValue.MachineNameList.Add("SH-Y-003");
                returnValue.RunTimeList.Add(76);
                returnValue.PowerTimeList.Add(0.7);

                returnValue.MachineNameList.Add("SH-Y-004");
                returnValue.RunTimeList.Add(69.6);
                returnValue.PowerTimeList.Add(0.52);

                returnValue.MachineNameList.Add("SH-Y-005");
                returnValue.RunTimeList.Add(69);
                returnValue.PowerTimeList.Add(0.5);

                returnValue.MachineNameList.Add("SH-Y-006");
                returnValue.RunTimeList.Add(56);
                returnValue.PowerTimeList.Add(0.2);

                returnValue.MachineNameList.Add("SH-C-007");
                returnValue.RunTimeList.Add(67);
                returnValue.PowerTimeList.Add(0.49);

                returnValue.MachineNameList.Add("SH-B-008");
                returnValue.RunTimeList.Add(69);
                returnValue.PowerTimeList.Add(0.5);

                returnValue.MachineNameList.Add("SH-D-009");
                returnValue.RunTimeList.Add(77);
                returnValue.PowerTimeList.Add(0.68);

                returnValue.MachineNameList.Add("SH-D-010");
                returnValue.RunTimeList.Add(69);
                returnValue.PowerTimeList.Add(0.5);

                returnValue.MachineNameList.Add("SH-D-011");
                returnValue.RunTimeList.Add(72);
                returnValue.PowerTimeList.Add(0.65);
  
            }
            else if (customName == "吉安钢铁")
            {
                returnValue.MachineNameList.Add("SH-D-010");
                returnValue.RunTimeList.Add(68);
                returnValue.PowerTimeList.Add(0.55);

                returnValue.MachineNameList.Add("SH-D-011");
                returnValue.RunTimeList.Add(73);
                returnValue.PowerTimeList.Add(0.66);
            }
            else if (customName =="包钢")
            {
                returnValue.MachineNameList.Add("BT-Y-001");
                returnValue.RunTimeList.Add(68);
                returnValue.PowerTimeList.Add(0.55);

                returnValue.MachineNameList.Add("BT-Y-002");
                returnValue.RunTimeList.Add(68);
                returnValue.PowerTimeList.Add(0.6);

                returnValue.MachineNameList.Add("BT-Y-003");
                returnValue.RunTimeList.Add(69);
                returnValue.PowerTimeList.Add(0.65);

                returnValue.MachineNameList.Add("BT-Y-004");
                returnValue.RunTimeList.Add(65);
                returnValue.PowerTimeList.Add(0.45);

                returnValue.MachineNameList.Add("BT-Y-004");
                returnValue.RunTimeList.Add(64);
                returnValue.PowerTimeList.Add(0.47);

                returnValue.MachineNameList.Add("BT-C-005");
                returnValue.RunTimeList.Add(66);
                returnValue.PowerTimeList.Add(0.47);

                returnValue.MachineNameList.Add("BT-T-006");
                returnValue.RunTimeList.Add(70);
                returnValue.PowerTimeList.Add(0.55);

                returnValue.MachineNameList.Add("BT-D-007");
                returnValue.RunTimeList.Add(69);
                returnValue.PowerTimeList.Add(0.85);

                returnValue.MachineNameList.Add("BT-D-008");
                returnValue.RunTimeList.Add(65);
                returnValue.PowerTimeList.Add(0.45);

                returnValue.MachineNameList.Add("BT-B-009");
                returnValue.RunTimeList.Add(70);
                returnValue.PowerTimeList.Add(0.75);
            }
  
            return returnValue;
        }

        public async Task HandleJson()
        {
            var obj = GetLocalSaleData();
            var sheet1 = obj["Sheet1"];
            foreach (var item in sheet1)
            {

                var user = item["User"].ToString();
                var product = item["Product"].ToString();
                var capacity = item["Capacity"].ToString();
                var qty = item["Qty"].ToString();
                var year = item["Year"].ToString();

                var isout = item["出口/国内"] == null ? false : true;
                var outContinent = item["出口洲名"] == null ? string.Empty : item["出口洲名"].ToString();
                var outCountry = item["出口/国内"] == null ? string.Empty : item["出口/国内"].ToString();
                try
                {

                    var userArr = user.Split(' ');
                    var productArr = product.Split(' ');
                    var capacityArr = capacity.Split(' ');
                    var qtyArr = qty.Split(' ');
                    var yearArr = year.Split(' ');


                    var cleanUser = userArr[0].Trim();
                    var cleanProduct = productArr[0].Trim();
                    var cleanCapacity = capacityArr[0].Trim();
                    var cleanQty = qtyArr[0].Trim();
                    var cleanYear = yearArr[0].Trim();

                    var cleanContinent = outContinent.Trim();
                    var cleanCountry = outCountry.Trim();


                    var intQty = Convert.ToInt32(cleanQty);
                    var intYear = Convert.ToInt32(cleanYear);

                    var entity = new SaleRecord()
                    {
                        Customer = cleanUser,
                        Product = cleanProduct,
                        Specification = cleanCapacity,
                        Quantity = intQty,
                        Year = intYear,
                        Province = string.Empty,
                        IsOut = isout,
                        Country = isout ? cleanCountry : string.Empty,
                        Continent = isout ? cleanContinent : string.Empty,
                    };

                    await saleRecordRepository.InsertAsync(entity);

                   // Logger.LogInformation($"{cleanUser},{cleanProduct},{cleanCapacity},{cleanQty},{cleanYear},{isout},{outCountry}");

                }
                catch (Exception ex)
                {
                    Logger.LogError($"{user},{product},{capacity},{qty},{year},{isout},{outCountry} 失败 {ex.Message}");
                    continue;

                }
            }


        }
 
        private JObject GetLocalSaleData()
        {
            var wwwrootDir = Path.Combine(_webHostEnv.ContentRootPath, "wwwroot");
            var cityJson = Path.Combine(wwwrootDir, "saleJson", "saleData.json");
            string jsonContent = File.ReadAllText(cityJson);
            var obj = JsonConvert.DeserializeObject<JObject>(jsonContent);
            return obj;
        }

        public async Task ImportExcelFile(IFormFile uploadedFile)
        {
            if (uploadedFile != null && !string.IsNullOrEmpty(uploadedFile.FileName))
            {
                using var package = new ExcelPackage(uploadedFile.OpenReadStream());
                try
                {
                    var workSheets = package.Workbook.Worksheets;
                    if (workSheets.Count != 0)
                    {
                        var data = GetVariableData1(workSheets[3]);

                        await SaveData(data);
                    }
                }
                catch (Exception ex)
                {
                    throw new UserFriendlyException($@"解析出错:{ex.Message}");
                }
            }
        }

        private static IEnumerable<SaleRecordDto> GetVariableData1(ExcelWorksheet excelWorksheet)
        {
            var returnValue = new List<SaleRecordDto>();

            for (var rowIterator = 12; rowIterator <= excelWorksheet.Dimension.End.Row; rowIterator++)
            {
                var item = new SaleRecordDto();

                var contractNo = excelWorksheet.Cells[rowIterator, 2].Value?.ToString()?.Trim();
                var customer = excelWorksheet.Cells[rowIterator, 4].Value?.ToString()?.Trim();
                var province = excelWorksheet.Cells[rowIterator, 5].Value?.ToString()?.Trim();
                var product = excelWorksheet.Cells[rowIterator, 7].Value?.ToString()?.Trim();
                var productName = excelWorksheet.Cells[rowIterator, 8].Value?.ToString()?.Trim();
                var count = excelWorksheet.Cells[rowIterator, 12].Value?.ToString()?.Trim();

                try
                {
                    item.Quantity = Convert.ToInt32(count);
                    item.Customer = customer;
                    item.ContractNo = contractNo;
                    item.Province = province;
                    item.Product = product;
                    item.ProductName = productName;
                    returnValue.Add(item);

                    Console.BackgroundColor = ConsoleColor.Green;
                    Console.WriteLine($@"{rowIterator} 解析成功,{item.ToJson()}");
                }
                catch (Exception ex)
                {
                    Console.BackgroundColor = ConsoleColor.Red;
                    Console.WriteLine($@"{rowIterator} 解析失败,{ex.ToString()}");
                    continue;
                }
            }

            return returnValue;
        }

        private async Task SaveData(IEnumerable<SaleRecordDto> dataList)
        {
            foreach (var entity in dataList.Select(item => ObjectMapper.Map<SaleRecordDto, SaleRecord>(item)))
            {
                await saleRecordRepository.InsertAsync(entity);
            }
        }
    }
}